package gbench.appdemo.myworld.junit;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;

import gbench.appdemo.myworld.market.SimpleMarket;
import gbench.appdemo.myworld.market.commodity.Commodity;
import gbench.appdemo.myworld.market.license.CommodityLicenseGroup;
import gbench.common.tree.LittleTree;
import gbench.common.tree.LittleTree.Elem;
import gbench.common.tree.LittleTree.IRecord;

import static gbench.appdemo.myworld.market.commodity.Commodity.COMMODITY;
import static gbench.common.tree.LittleTree.Tuple2.*;

/**
 * 
 * @author gbench
 *
 */
public class JunitDrcr {

    final Commodity grain = COMMODITY("粮食", "公斤");
    final Commodity wheal = COMMODITY("麦子", "公斤");
    final Commodity rmb = COMMODITY("RMB", "元");
    final Commodity labor = COMMODITY("产业劳动", "天");
    final Commodity bread = COMMODITY("面包", "个");

    @Test
    public void foo() {

        final var market = SimpleMarket.of("第一产业", grain);// 创建一个市场经济
        final var farm = market.getCommodityLicense("耕田", wheal);
        final var zhangsan = market.getCommodityLicense("张三", labor);
        final var lisi = market.getCommodityLicense("李四", rmb);
        zhangsan.debitB(farm, wheal, 100000, labor, 100);
        lisi.debitB(zhangsan, wheal, 1000, rmb, 6000);

        final var zhangsanRole = CommodityLicenseGroup.of("张三", zhangsan);
        zhangsanRole.getDoubleEntries().forEach(e -> {
            System.out.println(e);
        });

        zhangsanRole.trialBalance().forEach(e -> {
            System.out.println(e);
        });
    }

    /**
     * 获得商品列表
     * 
     * @return
     */
    public List<Commodity> commodities() {
        return Arrays.stream(this.getClass().getDeclaredFields())
        .filter(e -> Commodity.class.isAssignableFrom(e.getType())).map(e -> {
            try {
                final var value = (Commodity) e.get(this);
                return value;
            } catch (Exception e1) {
                e1.printStackTrace();
            } // try
            return null;
        }).filter(e -> e != null).collect(Collectors.toList());
    }

    @Test
    public void bar() {
        final var cc = commodities().toArray(Commodity[]::new);
        Elem.cph2(cc, cc).map(IRecord::L2REC)
        .map(e->e.set("name", e.get(0,Commodity.class).getName())) // 增加一个分类字段name
        .collect(IRecord.pvtclc("name"))
        .dfs_forall((p,ss)->{
            System.out.println(p+"--->"+ss);
        });
    }
    
    @Test
    public void qux() {// 生成一份汇率表
        
        final var cc = commodities().toArray(Commodity[]::new);
        Elem.cph2(cc, cc).map(IRecord::L2REC)
        .filter(LittleTree.DISTINCT_BY_KEY( e-> Stream.of(e.get(0,Commodity.class),e.get(1,Commodity.class)).sorted()
            .map(Object::toString).collect(Collectors.joining("")) // 字符串链接生成key 
        )).map(e->{
            final var a = e.get(0,Commodity.class);
            final var b = e.get(1,Commodity.class);
            return SimpleMarket.price(a, b, a.equals(b)?1:Math.random());
        }).forEach(e->{
            System.out.println(e);
        });
        
    }
    
    @Test
    public void boo() {
        final var broker = SimpleMarket.of("第一产业", grain)
            .getTradeBroker(TUP2("稻田",wheal),TUP2("农民",wheal),TUP2("厨师",bread));// 制作一个交易代理。
        
        broker.setCurrent("农民").purchase("稻田", 1000,10000);
        broker.purchase("厨师", bread, 100, rmb, 30);
        
        System.out.println("\n农民");
        broker.trialBalance().forEach(e->{
            System.out.println(e);
        });
        
        System.out.println("\n农民");
        broker.setCurrent("稻田").trialBalance().forEach(e->{
            System.out.println(e);
        });
        
        System.out.println("\nentries");
        broker.getDoubleEntries().forEach(e->{
            System.out.println(e);
        });
        
        System.out.println("\nentries");
        broker.setCurrent("农民").getDoubleEntries().forEach(e->{
            System.out.println(e);
        });
    }

}
